<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

/**
 * Collection.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @package Simplify_Kernel_Data
 */
class ItemCollection
{

  /**
   *
   * @var array
   */
  protected $items;

  /**
   * Constructor.
   *
   * @return void
   */
  public function __construct()
  {
    $this->reset();
  }

  /**
   * Add $item to collection.
   *
   * @param $item mixed The item.
   * @return void
   */
  public function addItem($item)
  {
    $this->items[] = $item;
  }

  /**
   * Add $item to collection at $index position.
   *
   * @param $item mixed The item.
   * @param $index int The index.
   * @return void
   */
  public function addItemAt($item, $index)
  {
    if ($index < 0 || $index >= count($this->items)) {
      throw new ParameterException('Supplied index is out of bounds');
    }
    
    array_splice($this->items, $index, 0, $item);
  }

  /**
   * Check if $item exists in collection.
   *
   * @param $item The item.
   * @return boolean True if item exists, false otherwise.
   */
  public function contains($item)
  {
    try {
      $this->getItemIndex($item);
      return true;
    } catch ( NotFoundException $e ) {
      return false;
    }
  }

  /**
   * Get item from current position in collection.
   *
   * @param $index int The index.
   * @return mixed The item.
   */
  public function getItemAt($index)
  {
    if ($index < 0 || $index >= count($this->items)) {
      throw new ParameterException('Supplied index is out of bounds');
    }
    
    return $this->items[$index];
  }

  /**
   * Get the position of $item in the collection.
   *
   * @param $item mixed The item.
   * @return int The index.
   */
  public function getItemIndex($item)
  {
    $index = 0;
    
    while ( isset($this->items[$index]) && $this->items[$index] != $item ) {
      $index ++;
    }
    
    if ($index >= count($this->items)) {
      throw new NotFoundException('Item not found in collection');
    }
    
    return $index;
  }

  /**
   * Remove $item from collection and return it.
   *
   * @param $item mixed The item.
   * @return mixed The item.
   */
  public function removeItem($item)
  {
    $index = $this->getItemIndex($item);
    return $this->removeItemAt($index);
  }

  /**
   * Remove the item at $index position from the collection.
   *
   * @param $index int The index.
   * @return mixed The item.
   */
  public function removeItemAt($index)
  {
    if ($index < 0 || $index >= count($this->items)) {
      throw new ParameterException('Supplied index is out of bounds');
    }
    
    $items = array_splice($this->items, $index, 1);
    
    return $items[0];
  }

  /**
   * Reset (delete all items from) the collection.
   *
   * @return void
   */
  public function reset()
  {
    $this->items = array ();
  }

  /**
   *
   *
   * @return int
   */
  public function size()
  {
    return count($this->items);
  }
}

?>